/**
 *  *2010-10-22 上午11:03:45  add 
 */
package com.base.myproject.client.init;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseLiveView;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

/**
 * @author Administrator 2010-10-22 javazgw@gmail.com
 */
public class Test extends ContentPanel {
	  final PagingToolBar toolBar = new PagingToolBar(100);
	public Test() {
		add(createGrid());
		 setBottomComponent(toolBar);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("cname");
		type.getField("cname").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("ccode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select ccode,cname from ccode");
		//info.setLimit("50");
//		info.setNeedAllRow("true");
		
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		BasePagingLoader loader = (BasePagingLoader)store.getLoader();
		/* loader.addListener(Loader.BeforeLoad, new Listener<LoadEvent>() {
		      public void handleEvent(LoadEvent be) {
		        BasePagingLoadConfig m = be.<BasePagingLoadConfig> getConfig();
		        
		        GWT.log("load before...");
		        m.set("start", m.get("offset"));
		        m.set("ext", "js");
		        m.set("lightWeight", true);
		        m.set("sort", (m.get("sortField") == null) ? "" : m.get("sortField"));
		        m.set("dir",
		            (m.get("sortDir") == null || (m.get("sortDir") != null && m.<SortDir> get(
		                "sortDir").equals(SortDir.NONE))) ? "" : m.get("sortDir"));

		      }
		    });
		 
		 loader.setSortDir(SortDir.DESC);
		    loader.setSortField("ccode");

		    loader.setRemoteSort(true);
		    
		  */
		    toolBar.bind(loader);
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		//CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	//	columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("ccode", "ccode", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("cname", "cname", 80);
		columns.add(C2);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		//cbs.setSelectionMode(SelectionMode.SINGLE);
		//grid.setSelectionModel(cbs);
		
/*		 BufferView view = new BufferView();
		    view.setScrollDelay(0);
		    view.setRowHeight(34);
		    grid.setView(view);   */
		  
		BaseLiveView liveView = new BaseLiveView();  
	    liveView.setEmptyText("No rows available on the server.");  
	   // liveView.setRowHeight(32);  
	    liveView.setCacheSize(100);
	    grid.setView(liveView);  
		grid.setHeight(400);
		GridSelectionModel<BusiObject> gsm =new GridSelectionModel<BusiObject>();
		gsm.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(gsm);
	/*	   grid.addListener(Events.Attach, new Listener<GridEvent<BusiObject>>() {
		        public void handleEvent(GridEvent<BusiObject> be) {
		        	GWT.log("li........");
		        	((BasePagingLoader)store.getLoader()).load(0, 50);
		        }
		      });*/
		  // grid.getAriaSupport().setDescribedBy(toolBar.getId() + "-display");  
		return grid;
	}
}
 